package com.thinker.checkorder.service.impl;

import com.thinker.checkorder.bean.RepeatParam;
import com.thinker.checkorder.dao.RepeatDao;
import com.thinker.checkorder.service.RepeatService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * RepeatServiceImpl
 *
 * @author zhougf
 * @date 2019/9/16
 */
@Service
public class RepeatServiceImpl implements RepeatService{

    @Resource
    private RepeatDao repeatDao;



    private Long id;

    @Override
    public List<RepeatParam> listRepeatParam(String today) {
        return repeatDao.listRepeatParam(today);
    }

    @Override
    @Async
    public void add(Integer id) {

        System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务：" + id);
        int count = 5;
        while (count >= 1) {
            insert(id);
            count--;
        }

    }

    @Override
    public synchronized Integer insert(Integer id) {
            System.out.println(id);
            int count = repeatDao.countById("zhou");
            if (count < 30) {
                return repeatDao.add(id);
            }
            return  1;
    }

}
